﻿namespace Triad.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Triad.Common;
    using Triad.DAL;
    using Triad.Model;
    
    /// <summary>
    /// General Functions in the Business Logic Layer
    /// </summary>
    public class framework
    {
        private string c_ErrorMessage = string.Empty;

        /// <summary>
        /// Gets or sets the error message.
        /// </summary>
        /// <value>The error message.</value>
        public string ErrorMessage
        {
            get
            {
                return c_ErrorMessage;
            }
            set
            {
                c_ErrorMessage = value;
            }
        }

        #region Get Methods


        /// <summary>
        /// Gets the user by employee number.
        /// </summary>
        /// <param name="p_UserName">The employee number.</param>
        /// <returns></returns>
        public virtual WebUser GetUserByUserName(string p_UserName)
        {
            return frameworkDBSProcs.Create().GetUserByUserName(p_UserName);
        }


        /// <summary>
        /// Gets the user object.
        /// </summary>
        /// <param name="p_UserParam">The user param.</param>
        /// <param name="qType">Type of the q.</param>
        /// <returns></returns>
        public virtual Dictionary<string, string> GetUserObject(string p_UserParam, UserQueryType qType)
        {

            Dictionary<string, string> Dict = new Dictionary<string, string>
                                                        {
                                                            {"EmpNumber", "111"},
                                                            {"Theme", "Red"},
                                                            {"MachineID", "system"},
                                                            {"WebUserId", "441234"},
                                                            {"Culture", "en-US"},
                                                            {"Sid", "Sid"},
                                                            {"UserName", "UserName"}
                                                        };

            return Dict;

            //WebUser qTemp = null;
            //switch (qType)
            //{
            //    case UserQueryType.EmployeeId:
            //        qTemp = frameworkDBSProcs.Create().GetUserByEmployeeNumber(p_UserParam);
            //        break;
            //    case UserQueryType.UserId:
            //        qTemp = frameworkDBSProcs.Create().GetUserByUserId(p_UserParam);
            //        break;
            //}

            //if (qTemp != null)
            //{
            //    Dictionary<string, string> Dict = new Dictionary<string, string>
            //                                            {
            //                                                {"EmpNumber", qTemp.TriadEmployeeNumber},
            //                                                {"Theme", qTemp.Theme},
            //                                                {"MachineID", "system"},
            //                                                {"WebUserId", Convert.ToString(qTemp.WebUserId)},
            //                                                {"Culture", qTemp.CultureName},
            //                                                {"Sid", qTemp.SidNumber},
            //                                                {"UserName", qTemp.UserName},
            //                                                {"Site", qTemp.Site},
            //                                                {"SiteFF", qTemp.SiteFocusedFactory},
            //                                                {"SiteId", Convert.ToString(qTemp.SiteId)},
            //                                                {"FFId", Convert.ToString(qTemp.FocusedFactoryId)}
            //                                            };

            //    return Dict;
            //}
            //return null;
        }

        /// <summary>
        /// Gets the navigation menu items.
        /// </summary>
        /// <param name="p_Culture">The culture.</param>
        /// <param name="p_visibleOnly">if set to <c>true</c> show the menu item.</param>
        /// <returns></returns>
        /// <remarks>
        /// Author: Tim Fischer,   11/14/2008 - 8:29 PM
        /// Modified By:
        /// </remarks>
        public IEnumerable<usp_MenuItems_SelectResult> getMenuItems(string p_Culture, bool p_visibleOnly)
        {
            return frameworkDBSProcs.Create().GetMenuItems(p_Culture, true, false).ToArray();
        }

        /// <summary>
        /// Gets all users.
        /// </summary>
        /// <returns></returns>
        public WebUser[] GetAllUsers()
        {
            return frameworkDBSProcs.Create().GetAllUsers();
        }

        #endregion

        #region Insert Methods

        /// <summary>
        /// Inserts a new user.
        /// </summary>
        /// <param name="user">The user.</param>
        public int InsertUser(WebUser user)
        {

            OperationStatus opStatus = frameworkDBSProcs.Create().InsertUser_Direct(user);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in Triad.BLL.framework.InsertUser: {0}", opStatus.Exception.Message));
            }
            return user.WebUserId;
        }



        /// <summary>
        /// Inserts the user.
        /// </summary>
        /// <param name="p_UserName">The user name.</param>
        /// <param name="p_SidNumber">The sid number.</param>
        /// <param name="p_TriadEmployeeNumber">The Triad employee number.</param>
        /// <param name="p_CultureName">The culture name.</param>
        /// <param name="p_SiteId">The site id.</param>
        /// <param name="p_FocusedFactoryId">The focused factory id.</param>
        /// <param name="p_UserIdCreatedBy">The user id created by.</param>
        /// <param name="p_StationCreatedAt">The station created at.</param>
        /// <param name="p_UserIdModifiedBy">The user id modified by.</param>
        /// <param name="p_StationModifiedAt">The station modified at.</param>
        /// <param name="p_MesUserId">The mes user id.</param>
        /// <returns></returns>
        public int InsertUser(string p_UserName, string p_SidNumber, string p_TriadEmployeeNumber, string p_CultureName, int p_SiteId, int p_FocusedFactoryId, int p_UserIdCreatedBy, string p_StationCreatedAt, int? p_UserIdModifiedBy, string p_StationModifiedAt, int? p_MesUserId)
        {
            //var user = new MesUser
            //{
            //    UserName = p_UserName,
            //    TriadEmployeeNumber = p_TriadEmployeeNumber,
            //    SiteId = p_SiteId,
            //    UserIdCreatedBy = p_UserIdCreatedBy,
            //    UserIdModifiedBy = p_UserIdModifiedBy,
            //    SidNumber = p_SidNumber,
            //    StationCreatedAt = p_StationCreatedAt,
            //    StationModifiedAt = p_StationModifiedAt,
            //    CultureName = p_CultureName,
            //    DateCreatedUtc = DateTime.Now,
            //    FocusedFactoryId = p_FocusedFactoryId
            //};


            //OperationStatus opStatus = GeneralDBSProcs.Create().InsertUser_Direct(user);
            //if (!opStatus.Status)
            //{
            //    throw new ApplicationException(String.Format("Error occurred in MESWebBLL.General.InsertUser: {0}", opStatus.Exception.Message));
            //}
            return 0; // user.MesUserId;
        }

        public int InsertMenuItem_USP(string p_MenuLabel, int p_MenuItemIdParent, bool p_Visible, short p_SortOrder, string p_TargetUrl)
        {
            //var user = new MesUser
            //{
            //    UserName = p_UserName,
            //    TriadEmployeeNumber = p_TriadEmployeeNumber,
            //    SiteId = p_SiteId,
            //    UserIdCreatedBy = p_UserIdCreatedBy,
            //    UserIdModifiedBy = p_UserIdModifiedBy,
            //    SidNumber = p_SidNumber,
            //    StationCreatedAt = p_StationCreatedAt,
            //    StationModifiedAt = p_StationModifiedAt,
            //    CultureName = p_CultureName,
            //    DateCreatedUtc = DateTime.Now,
            //    FocusedFactoryId = p_FocusedFactoryId
            //};


            OperationStatus opStatus = frameworkDBSProcs.Create().InsertMenuItem_USP(p_MenuLabel,p_MenuItemIdParent,p_Visible,p_SortOrder,p_TargetUrl);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in Triad.BLL.framework.InsertUser: {0}", opStatus.Exception.Message));
            }
                return 0;
        }
        #endregion

        #region Update Methods
        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="userObj">The user obj.</param>
        /// <returns></returns>
        public bool UpdateUser(store.User userObj)
        {
            //ApplyUserRules(user);

            OperationStatus opStatus = frameworkDBSProcs.Create().UpdateUser(userObj);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in Triad.BLL.framework.UpdateUser: {0}", opStatus.Exception.Message));
            }
            return true;
        }

        public bool UpdateMenuItem(int p_MenuItemId, string p_MenuLabel, int p_ReferenceIdMenuItemLabel, string p_Culture, bool p_Visible, short p_SortOrder, string p_TargetUrl)
        {
            OperationStatus opStatus = frameworkDBSProcs.Create().UpdateMenuItem(p_MenuItemId,p_MenuLabel,p_ReferenceIdMenuItemLabel,p_Culture,p_Visible,p_SortOrder,p_TargetUrl);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in Triad.BLL.framework.UpdateMenuItem: {0}", opStatus.Exception.Message));
            }
            return true;
        }
        #endregion

        #region Delete Methods

        public bool? DeleteMenuItem(int p_MenuItemId)
        {
            return frameworkDBSProcs.Create().DeleteMenuItem(p_MenuItemId);
        }

        #endregion

        #region BusinessRules



        #endregion


    }

}
